package pbbltobpel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import pbbltobpel.parser.PddlPlan;

public class Gui extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	JButton selectXmlFileButton, addWsdlFileButton, removeWsdlFileButton;
	JTextField selectedXmlPath;
	JList wsdlList;
	JFileChooser fc;
	HashMap<String, String> wsdls;
	
	JButton loadButton, saveBpel;
	
	PddlPlan plan;
	
	public Gui() {
		//super(new BorderLayout());
		
		setLayout(new BorderLayout());
		wsdls = new HashMap<String, String>();

		JPanel paramsPanel = new JPanel();
		paramsPanel.setLayout(new BoxLayout(paramsPanel, BoxLayout.Y_AXIS));
		
		selectXmlFileButton = new JButton("^");
		selectXmlFileButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				fc.setMultiSelectionEnabled(false);
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				int ret = fc.showOpenDialog(Gui.this);
				if ( ret == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();
					selectedXmlPath.setText(file.getAbsolutePath());
				}
			}
		});
		
		addWsdlFileButton = new JButton("Add");
		addWsdlFileButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				fc.setMultiSelectionEnabled(true);
				fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				int ret = fc.showOpenDialog(Gui.this);
				if ( ret == JFileChooser.APPROVE_OPTION) {
					File[] files = fc.getSelectedFiles();
					for(File file : files){
						wsdls.put(file.getName(),file.getAbsolutePath());
						wsdlList.setListData(wsdls.keySet().toArray());
					}
				}
			}
		});
		
		removeWsdlFileButton = new JButton("Remove");
		removeWsdlFileButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String selected = (String)wsdlList.getSelectedValue();
				if(wsdls.containsKey(selected))
					wsdls.remove(selected);
				wsdlList.setListData(wsdls.keySet().toArray());
			}
		});
		
		selectedXmlPath = new JTextField();
		selectedXmlPath.setSize(new Dimension(500, 20));
		selectedXmlPath.setMinimumSize(new Dimension(500, 20));
		selectedXmlPath.setEditable(false);
		
		fc = new JFileChooser();
		
		
		JPanel xmlPanel = new JPanel();
		xmlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		xmlPanel.setSize(1000, 20);
		xmlPanel.add(new Label("PDDL XML File:"));
		xmlPanel.add(selectedXmlPath);
		xmlPanel.add(selectXmlFileButton);
		
		paramsPanel.add(xmlPanel);
		
		
		JPanel wsdlPanel = new JPanel();
		wsdlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		wsdlPanel.add(new Label("WSDL Files:"));
		wsdlPanel.add(addWsdlFileButton);
		wsdlPanel.add(removeWsdlFileButton);
		
		paramsPanel.add(wsdlPanel);
		
		JPanel wsdlListPanel = new JPanel();
		wsdlListPanel.setBorder(BorderFactory.createEtchedBorder());
		wsdlListPanel.setSize(1000, 200);
		wsdlList = new JList(wsdls.keySet().toArray());
		wsdlList.setVisibleRowCount(10);
		wsdlListPanel.add(wsdlList);
		
		paramsPanel.add(wsdlListPanel);
		
		add(paramsPanel, BorderLayout.NORTH);
		
		JPanel optionsPanel = new JPanel();
		
		loadButton = new JButton("LOAD");
		loadButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try{
					plan = program.Load(selectedXmlPath.getText(), wsdls.values().toArray());
					saveBpel.setEnabled(true);
				}
				catch(Exception e){
					e.printStackTrace();
					JOptionPane.showMessageDialog(Gui.this, e);
				}
				
			}
		});
		optionsPanel.add(loadButton);
		
		final ParameterTypeMapper mapper = new ParameterTypeMapper();
		
		saveBpel = new JButton("SAVE");
		saveBpel.setEnabled(false);
		saveBpel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int ret = fc.showSaveDialog(Gui.this);
				if(ret == JFileChooser.APPROVE_OPTION){
					try{
					program.Save(plan.getFileName(), Converter.Convert(plan, mapper).getDocument() , 
							WSDLGenerator.getWSDLFile(plan, mapper), Deployer.getDeployFile(plan),
							fc.getSelectedFile().getAbsolutePath());
					}
					catch(Exception e){
						e.printStackTrace();
						JOptionPane.showMessageDialog(Gui.this, e.getMessage());
					}
				}
			}
		});
		optionsPanel.add(saveBpel);
		
		add(optionsPanel, BorderLayout.SOUTH);
		
	}
}
